package com.kitty.game.gm.handler;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.gm.GmService;
import com.kitty.game.role.model.Role;
import com.kitty.mina.message.MessagePusher;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

public class GmUnlock implements GMFather {

    @Override
    public boolean handle(Role aaaa, String content) {
        GmService gmService = SpringUtils.getBean(GmService.class);
        String name = gmService.replaceName(content);
        String sql = "select uid from t_role where name = @name";
        Sql sql1 = Sqls.create(sql);
        sql1.setParam("name", name);
        sql1.setCallback(new SqlCallback() {
            @Override
            public Object invoke(Connection connection, ResultSet resultSet, Sql sql) throws SQLException {
                if (resultSet.next()) {
                    return resultSet.getLong("uid");
                }
                return 0;
            }
        });
        SpringUtils.getBean(Dao.class).execute(sql1);
        long uid = sql1.getLong();
        {
            Role role1 = SpringUtils.getPlayerService().getPlayerBy(uid);
            role1.setSafePasswdResetStartTime(0);
            role1.setSafePasswd(null);
            role1.save();
        }
        MessagePusher.pushMessage(aaaa, new RespNotifyMiscEx("解锁密码成功"));
        return true;
    }
}
